package cn.bonoon.kernel.support;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.context.ApplicationContext;

import cn.bonoon.kernel.bytecode.BytecodeFactory;
import cn.bonoon.kernel.bytecode.EntityWriter;
import cn.bonoon.kernel.support.entities.Persistable;
import cn.bonoon.kernel.support.searcher.StandardSearcher;

public class StandardOperator extends StandardSearcher{
	/**
	 * <pre>
	 * 一般来说，普通实往数据库写值的只有一个，即相应的编辑实体，或者可能存在多个，
	 * 这里应该考虑少量的情况，并且对这种情况进行优化处理
	 * </pre>
	 */
	private final Map<Class<?>, EntityWriter> valueWriters;
	
	public StandardOperator(Class<? extends Persistable> entityClass, ApplicationContext applicationContext) {
		super(entityClass, applicationContext);
		valueWriters = new ConcurrentHashMap<Class<?>, EntityWriter>(INITIAL_CAPACITY, LOAD_FACTOR, CONCURRENCY_LEVEL);

	}
	public EntityWriter getEntityWriter(Class<?> cls) throws Exception{
		EntityWriter sc = valueWriters.get(cls);
		if(null == sc){
			sc = BytecodeFactory.writer(applicationContext, cls, entityDescriptor);
			valueWriters.put(cls, sc);
		}
		return sc;
	}
}
